package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.XhsStore;
import com.macro.mall.service.XhsStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "StoreController")
@Tag(name = "StoreController", description = "门店管理")
@RequestMapping("/xhs/store")
@Validated
public class StoreController {

    @Autowired
    private XhsStoreService xhsStoreService;

    @ApiOperation("分页查询门店列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<XhsStore>> getStoreList(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "20") Integer size,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "city", required = false) String city,
            @RequestParam(value = "poiId", required = false) String poiId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "noPhone", required = false) Boolean noPhone) {
        
        List<XhsStore> storeList = xhsStoreService.getStoreList(page, size, name, city, poiId, phone, noPhone);
        int total = xhsStoreService.getStoreCount(name, city, poiId, phone, noPhone);
        
        return CommonResult.success(CommonPage.restPage(storeList, Long.valueOf(total), page, size));
    }

    @ApiOperation("获取城市列表")
    @GetMapping("/cities")
    public CommonResult<List<String>> getCityList() {
        List<String> cityList = xhsStoreService.getCityList();
        return CommonResult.success(cityList);
    }

    @ApiOperation("根据ID查询门店详情")
    @GetMapping("/{id}")
    public CommonResult<XhsStore> getStoreById(@PathVariable Long id) {
        XhsStore store = xhsStoreService.getById(id);
        if (store == null) {
            return CommonResult.failed("门店不存在");
        }
        return CommonResult.success(store);
    }

    @ApiOperation("修改门店手机号")
    @PostMapping("/updatePhone/{id}")
    public CommonResult<Boolean> updateStorePhone(
            @PathVariable @NotNull Long id,
            @RequestBody Map<String, String> request) {
        
        String phone = request.get("phone");
        
        // 手机号格式验证（如果提供了手机号）
        if (phone != null && !phone.isEmpty() && !phone.matches("^1[3-9]\\d{9}$")) {
            return CommonResult.failed("手机号格式不正确");
        }
        
        // 检查门店是否存在
        XhsStore existingStore = xhsStoreService.getById(id);
        if (existingStore == null) {
            return CommonResult.failed("门店不存在");
        }
        
        // 检查手机号是否已被其他门店绑定
        if (phone != null && !phone.isEmpty()) {
            XhsStore storeWithPhone = xhsStoreService.getByPhone(phone);
            if (storeWithPhone != null && !storeWithPhone.getId().equals(id)) {
                return CommonResult.failed("该手机号已被其他门店绑定，同一手机号不能绑定两个门店");
            }
        }
        
        // 更新手机号
        int result = xhsStoreService.updatePhone(id, phone);
        if (result > 0) {
            return CommonResult.success(true);
        } else {
            return CommonResult.failed("修改失败");
        }
    }
}
